package com.bstek.pms.tools.service.impl;

import com.bstek.pms.tools.model.DBProperties;
import com.bstek.pms.tools.model.TableInfo;
import com.bstek.pms.tools.service.DBInfoService;
import com.bstek.pms.tools.service.ExportDocument;
import com.bstek.pms.tools.utils.DbUtils;
import com.bstek.pms.tools.utils.FileUtils;
import com.bstek.pms.tools.utils.PoiUtils;
import org.apache.commons.collections.MapUtils;

import java.io.File;
import java.io.InputStream;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description 导出多execl
 * @Author zhangkun
 * @CreateDate 2020/8/1
 */
public class ExportDocumentForMultipleExcel implements ExportDocument {

    //数据库连接参数
    private DBProperties properties;

    //项目参数
    private Map<String,Object> params;


    public ExportDocumentForMultipleExcel(){

    }

    public ExportDocumentForMultipleExcel(DBProperties properties, Map<String, Object> params) {
        this.properties = properties;
        this.params = params;
    }

    @Override
    public void export() throws Exception {

        //生成文件输出目录
        String exportFilePath = FileUtils.generateExportDirs(params.get("sysName") + "_" + "数据结构变更");

        //计算选中的数据行,从数据库查询表结构，写入到excel中
        List<TableInfo> selectedTabs = DBInfoService.getSelectedTabs(properties, params);

        //输出文档
        outputDocumentForModels(selectedTabs, exportFilePath);


    }

    /**
     * 通过模板生成文档
     * @param selectedTabs
     * @param exportFilePath
     * @throws Exception
     */
    private void outputDocumentForModels(List<TableInfo> selectedTabs, String exportFilePath) throws Exception {
        List<Map<String, Object>> summaryList = new ArrayList<>();//汇总信息列表
        int tabNum = 0; //表序号
        Map<String, Object> summaryParam = null;
        Connection conn = DbUtils.getConnection(properties);
        try{
            for (TableInfo selectedTab : selectedTabs) {
                Map<String, Object> colParam = DBInfoService.getColMap(selectedTab, conn, params,properties.getDbUserName());
                String tableName = selectedTab.getTableName().toUpperCase();
                summaryParam = putSummaryValues(summaryList, ++tabNum, colParam,tableName);
                InputStream inputStream = getClass().getClassLoader().getResourceAsStream("resources/excel/PmsDataFormationChange.xlsx");
                PoiUtils.generateExcelFileWithModel(exportFilePath+ File.separator+ tableName + ".xlsx",colParam,inputStream);
            }
        }finally {
            DbUtils.closeConn(conn);
        }
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream("resources/excel/PmsDataDictionaryList.xlsx");
        if(summaryParam != null) PoiUtils.generateExcelFileWithModel(exportFilePath+ File.separator + params.get("sysName") +"_"+ params.get("projectNo") +"数据字典目录.xlsx",summaryParam,inputStream);
    }


    /**
     * 生成汇总信息表的内容
     * @param summaryList
     * @param tabNum
     * @param colMap
     * @return
     */
    private Map<String, Object> putSummaryValues(List<Map<String, Object>> summaryList, int tabNum, Map<String, Object> colMap, String tn) {
        Map<String, Object> summaryValues = new HashMap<>(); //汇总信息
        summaryValues.put("tabNum", tabNum);
        summaryValues.put("sysName", params.get("sysName"));
        summaryValues.put("sysNo", params.get("sysNo"));
        summaryValues.put("subSysName", params.get("subSysName"));
        summaryValues.put("caseName", properties.getDbUserName());
        summaryValues.put("tableName", tn);
        List<Map<String,Object>> pmsDataFormationChange = (List<Map<String,Object>>)colMap.get("pmsDataFormationChange");
        String tableCname = null;
        if(pmsDataFormationChange != null && pmsDataFormationChange.size() > 0){
            tableCname = (String)pmsDataFormationChange.get(0).get("tableCname");
        }

        summaryValues.put("tableCname", tableCname);
        summaryList.add(summaryValues);

        Map<String, Object> summaryParam = MapUtils.putAll(new HashMap<String, Object>(), new Object[] {
                "pmsDataList",summaryList});
        return summaryParam;
    }



}
